﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winform客户端
{
    public partial class 服务器 : Form
    {
        public class MyClass
        {
            public Socket Socket;
            public override string ToString()
            {
                return Socket.RemoteEndPoint.ToString();
            }
        }

        
        public 服务器()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Socket serverSocket=new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream, 
                ProtocolType.Tcp);

            //绑定
            serverSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"),11811));

            //监听

            serverSocket.Listen(10);
            textBox1.Text = "服务器以启动\r\n";


            //接受客户端请求
            Thread accepThread=new Thread((obj) =>
            {
                Socket acceptThreadSocket = obj as Socket;
                while (true)
                {
                    Socket clientSocket = acceptThreadSocket.Accept();//会阻塞,一定要在while循环,连接一次,循环一回

                    textBox1.Invoke(new Action<String>(s =>
                    {
                        textBox1.Text += s + "\r\n";
                    }
                           ),clientSocket.RemoteEndPoint+"连接成功");

                    listBox1.Invoke(new Action<MyClass>(s =>
                    {
                        listBox1.Items.Add(s);
                    }), new MyClass
                    {
                        Socket = clientSocket
                    });
                    //接受客户端信息
                    Thread receiveThread=new Thread((obj2) =>
                    {
                        Socket receiveThreadSocket = obj2 as Socket;
                       
                        byte[] bs=new byte[1024];
                        int len = 0;
                        while ((len=receiveThreadSocket.Receive(bs))>0)//也会阻塞,所以不能放在accept线程中,因为不能因为接受一个信息
                            //阻塞了其他客户端连接
                        {
                            //处理信息   
                            textBox1.Invoke(new Action<String>(s =>
                            {
                                textBox1.Text += s + "\r\n";
                            }), receiveThreadSocket.RemoteEndPoint+":"+Encoding.UTF8.GetString(bs,0,len));
                        }
                    });
                    receiveThread.IsBackground = true;
                    receiveThread.Start(clientSocket);
                }
            });
            accepThread.IsBackground = true;
            accepThread.Start(serverSocket);

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MyClass sendSocket = (MyClass)listBox1.SelectedItem;
            sendSocket.Socket.Send(Encoding.UTF8.GetBytes(textBox2.Text));
        }
    }
}
